package com.btkjgc.service.impl;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.btkjgc.controller.CommonInfoController.CommonInfoUpdateForm;
import com.btkjgc.models.CommonInfo;
import com.btkjgc.service.intf.CommonInfoService_Intf;
import com.btkjgc.utils.HibernateUtil;

/**
 * Created by ray on 14-3-25.
 */
@Component
public class CommonInfoService_Impl implements CommonInfoService_Intf
{
	static Logger logger = Logger.getLogger(CommonInfoService_Impl.class);

	private final String MODEL_TABLE = "CommonInfo";

	public CommonInfo getCommonInfo()
	{
		String hql = "from " + MODEL_TABLE + " where id = ?";
		String param[] = { String.valueOf(0) };
		CommonInfo info = (CommonInfo) HibernateUtil.objectQuery(hql, param);
		return info;
	}

	@Override
	public int getCount(String batteryid)
	{
		return 0;
	}

	@Override
	public List<CommonInfo> getRecord(Integer pageIndex, Integer pageSize,
			String batteryid, String sortfield)
	{
		return null;
	}

	@Override
	public void updateCommonInfoById(Integer id, CommonInfoUpdateForm form)
	{
		String hql = "from " + MODEL_TABLE + " where id = ? ";
		String param[] = { id.toString() };
		CommonInfo info = (CommonInfo) HibernateUtil.objectQuery(hql, param);
		PropertyDescriptor[] properties = PropertyUtils
				.getPropertyDescriptors(CommonInfo.class);
		for (PropertyDescriptor p : properties)
		{
			String pName = p.getName();
			try
			{
				if (pName.equals("id") || pName.equals("class"))
				{
					continue;
				}
				Object v = PropertyUtils.getProperty(form, pName);
				if (v != null)
				{
					PropertyUtils.setProperty(info, pName, v);
				}
			}
			catch (IllegalAccessException e)
			{
				e.printStackTrace();
			}
			catch (InvocationTargetException e)
			{
				e.printStackTrace();
			}
			catch (NoSuchMethodException e)
			{
				e.printStackTrace();
			}
		}
		HibernateUtil.update(info);

	}
}